package com.htc.model.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.htc.constants.Constants;
import com.htc.model.dto.FitnessClassDTO;

public class FitnessClassModel {
	public static FitnessClassModel instance;

	public static FitnessClassModel getInstance() {
		if (instance == null)
			instance = new FitnessClassModel();
		return instance;
	}

	public boolean checkDB(Context context, String id) {
		boolean flag = false;
		String sql = "SELECT fitness_class_id FROM "
				+ Constants.TBL_FITNESS_CLASS + " where fitness_class_id ='"
				+ id + "'";
		DBHelper db = DBHelper.getInstance(context);
		Cursor c = db.Excutequery(sql);
		while (c.moveToNext()) {
			flag = true;
			break;
		}
		c.close();
		db.close();
		return flag;
	}

	public boolean insertFitClass(FitnessClassDTO fit, Context context) {
		boolean flag = false;
		if (!checkDB(context, fit.getFitnessClassId())) {
			String DATABASE_TABLE = Constants.TBL_FITNESS_CLASS;
			ContentValues values = new ContentValues();
			values.put("fitness_class_id", fit.getFitnessClassId());
			values.put("fitness_class_name", fit.getFitnessClassName());
			values.put("fitness_class_description",
					fit.getFitnessClassDescription());
			values.put("fitness_class_image", fit.getFitnessClassImage());
			values.put("priority", fit.getPriority());
			values.put("fitness_class_group_id", fit.getFitnessClassGroupId());
			values.put("fitness_class_name_ar", fit.getFitnessClassNameAr());
			values.put("fitness_class_description_ar",
					fit.getFitnessClassDescriptionAr());
			DBHelper db = DBHelper.getInstance(context);
			flag = db.ExcuteInsert(null, DATABASE_TABLE, values);
			db.close();
		}

		return flag;
	}

	public boolean updateFitClass(FitnessClassDTO fit, Context context) {
		boolean flag = false;

		String DATABASE_TABLE = Constants.TBL_FITNESS_CLASS;
		String where = "fitness_class_id='" + fit.getFitnessClassId() + "'";
		ContentValues values = new ContentValues();

		values.put("fitness_class_name", fit.getFitnessClassName());
		values.put("fitness_class_description",
				fit.getFitnessClassDescription());
		values.put("fitness_class_image", fit.getFitnessClassImage());
		values.put("priority", fit.getPriority());
		values.put("fitness_class_group_id", fit.getFitnessClassGroupId());
		values.put("fitness_class_name_ar", fit.getFitnessClassNameAr());
		values.put("fitness_class_description_ar",
				fit.getFitnessClassDescriptionAr());
		DBHelper db = DBHelper.getInstance(context);
		flag = db.ExcuteUpdate(null, DATABASE_TABLE, values, where, null);
		db.close();
		return flag;
	}

	public void deleteFitClass(FitnessClassDTO fit, Context context) {
		String sql = "fitness_class_id='" + fit.getFitnessClassId() + "'";
		DBHelper db = DBHelper.getInstance(context);
		String DATABASE_TABLE = Constants.TBL_FITNESS_CLASS;
		db.deleteTable(DATABASE_TABLE, sql);
		db.close();
	}
}
